<template>
	<view class="page">
		<uni-calendar 
			:insert="true"
			:lunar="true" 
			:start-date="'2024-3-2'"
			:end-date="'2039-5-20'"
			:selected="selected"
			@change="change"
			 />
			 
		 <view class="tn-margin tn-flex tn-flex-row-around tn-bg-white tn-radius">
		   <view class="tn-flex tn-flex-direction-column tn-flex-col-center  tn-padding-lg website-shadow" @click="tn('/pages/my/list')">
				<image src="https://mp-e3fbd118-dad0-4878-b40b-44597e7aa0eb.cdn.bspapp.com/icons/课程表.png" mode="widthFix" class="icon"></image>
			    <view class="tn-text-center tn-text-md">
				我的课程
			   </view>
			</view>
			<view class="tn-flex tn-flex-direction-column tn-flex-col-center tn-padding-lg website-shadow" @click="open(1)">
				<image src="https://mp-e3fbd118-dad0-4878-b40b-44597e7aa0eb.cdn.bspapp.com/icons/第二课堂icon.png" mode="widthFix" class="icon"></image>
				<view class="tn-text-center tn-text-md">
				课程复习
			   </view>
			</view>
			<view class="tn-flex tn-flex-direction-column tn-flex-col-center tn-padding-lg website-shadow" @click="open(2)">
				<image src="https://mp-e3fbd118-dad0-4878-b40b-44597e7aa0eb.cdn.bspapp.com/icons/我的学习.png" mode="widthFix" class="icon"></image>
				<view class="tn-text-center tn-text-md">
				全书复习
			   </view>
			</view>
		 </view>
		 <tn-select v-model="showSelectCourse" mode="single" :list="courses" @confirm="confirmCourses" title="选择复习课程"></tn-select>
		 <tn-select v-model="showSelectChapter" mode="single" :list="chapters" @confirm="confirmChapters" title="选择复习章节"></tn-select>
	</view>
</template>

<script>
	const obj=uniCloud.importObject('page-data-center')
	const db = uniCloud.database();
	/**
	 * 获取任意时间
	 */
	function getDate(date, AddDayCount = 0) {
		if (!date) {
			date = new Date()
		}
		if (typeof date !== 'object') {
			date = date.replace(/-/g, '/')
		}
		const dd = new Date(date)

		dd.setDate(dd.getDate() + AddDayCount) // 获取AddDayCount天后的日期

		const y = dd.getFullYear()
		const m = dd.getMonth() + 1 < 10 ? '0' + (dd.getMonth() + 1) : dd.getMonth() + 1 // 获取当前月份的日期，不足10补0
		const d = dd.getDate() < 10 ? '0' + dd.getDate() : dd.getDate() // 获取当前几号，不足10补0
		return {
			fullDate: y + '-' + m + '-' + d,
			year: y,
			month: m,
			date: d,
			day: dd.getDay()
		}
	}
	export default {
		data() {
			return {
				show:true,
				showSelectCourse:false,
				showSelectChapter:false,
				signList:[],
				selected:[],
				courses:[],
				chapters:[]
			}
		},
		onLoad() {
			 db.collection('opendb-sign-in').where('"user_id" == $env.uid').get().then((res)=>{
				 this.signList=res.result.data
				 res.result.data.forEach((item)=>{
					 this.selected.push({
						date: getDate(new Date(item.create_date),0).fullDate,
						info: '签到' 
					 })
				 })
			 })
		},
		methods: {
			async open(type){
				const order = db.collection('mydb-course-catch').where('"user_id" == $env.uid && invite == true').getTemp() // 注意结尾的方法是getTemp，对order表过滤得到临时表
				const courses = await db.collection(order, 'mydb-course-list').get() // 将获取的order表的临时表和book表进行联表查询
				this.courses=courses.result.data.map((item)=>{
					return {
						value:item.course_id[0]['_id'],
						label:item.course_id[0]['title']
					}
				})
				this.type=type
				this.showSelectCourse=true
			},
			async confirmCourses(e){
				let chapters=await db.collection("mydb-course-chapter").where(`"course_id" == "${e[0]['value']}"`).get()
				this.chapters=chapters.result.data.map((item)=>{
					return {
						value:item['_id'],
						label:item['title']
					}
				})
				if(this.type==1){
					this.showSelectChapter=true
				}else{
					uni.navigateTo({
						url:'/pages/sentence/sentence?id='+this.chapters[0]['value']
					})
				}
			},
			async confirmChapters(e){
				uni.navigateTo({
					url:'/pages/sentence/sentence?id='+e[0]['value']
				})
			},
			change(){
				
			},
			tn(url){
				uni.navigateTo({
					url
				})
			}
		}
	}
</script>

<style scoped>
page,.page{
	background-color: #f3f3f3;
	height: 100vh;
	overflow-y: scroll;
}
.icon{
	width: 48px;
}
</style>
